using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;
using System;

public class UIItemSlot : MonoBehaviour,IPointerDownHandler,IPointerEnterHandler,IPointerExitHandler
{
    [SerializeField] protected Image itemImage;
    [SerializeField] protected TextMeshProUGUI itemCountText;
    protected UI ui=>GetComponentInParent<UI>();
    public InventoryItem inventoryItem;
 
    public virtual void UpdateItemSlot(InventoryItem item)
    {
        inventoryItem = item;
        if (inventoryItem == null) return;
        itemImage.color = Color.white;
        if (inventoryItem.itemCount >= 0)
        {
            itemImage.sprite = inventoryItem.itemData.itemSprite;
            itemCountText.text = inventoryItem.itemCount.ToString();
        }
        else
        {
            itemImage.sprite = null;
            itemCountText.text = string.Empty;
        }
    }

    public virtual void OnPointerDown(PointerEventData eventData)
    {
        if (inventoryItem == null) return;
        ui.desctionTextBox.SetActive(false);
        if (Input.GetKeyDown(KeyCode.LeftControl)) Inventory.Instance.DeleteItem(inventoryItem.itemData);
        if (inventoryItem.itemData.itemType == ItemType.Equipment)
            Inventory.Instance.EquipItem(inventoryItem.itemData);
    }
    public void ClearSlot()
    {
        inventoryItem = null;
        itemImage.color = Color.clear;
        itemCountText.text = "";
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (inventoryItem == null|| inventoryItem.itemData==null) return;
        ui.desctionTextBox.SetActive(true);
        ui.desctionTextBox.GetComponentInChildren<TextMeshProUGUI>().text= inventoryItem.itemData.DestoryDescption();
        ui.desctionTextBox.GetComponentInChildren<TextMeshProUGUI>().text = 
            inventoryItem.itemData.itemName+"\n"+ inventoryItem.itemData.itemType+"\n"+inventoryItem.itemData.GetDescption();
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        ui. desctionTextBox.SetActive(false);
    }
}
